<script setup>
import {
  ArrowDownBold,
  Edit,
  Histogram,
  User,
  SwitchButton,
  PieChart,
  Menu,
  Operation,
  HelpFilled
} from '@element-plus/icons-vue';
import {ElMessageBox} from "element-plus";
import {markRaw, onMounted, reactive} from "vue";
import {getUserNameService, logOutService} from "@/api/admin.js";
import {removeToken} from "@/api/cookie.js";
import router from "@/router/index.js";

const loginUser = reactive({
  username: ''
})

onMounted(async () => {
  try {
    const loginUserResult = await getUserNameService()
    loginUser.username = loginUserResult.data
  } catch (error) {
    console.log(error)
    await router.push("/admin/login")
  }
})

async function logOut() {
  try {
    await ElMessageBox.confirm(
        '确定退出登录吗?',
        '温馨提示',
        {
          type: 'warning',
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          icon: markRaw(SwitchButton),
        }
    )
    await logOutService()
    removeToken()
    await router.push("/admin/login")
  } catch (error) {
    // 如果是用户取消操作，不需要显示错误
    if (error !== 'cancel') {
      console.log(rror)
    }
  }
}
</script>

<template>
  <el-container class="layout-container">
    <el-header class="el-header">
      <div class="class-header">
        <h1>求职训练平台 ToB 端管理</h1>
      </div>
      <el-dropdown placement="bottom-end">
          <span class="el-dropdown__box">
            <div>
              <strong>当前操作⽤⼾：{{ loginUser.username }}</strong>
            </div>
            <el-icon>
              <ArrowDownBold/>
            </el-icon>
          </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item @click="logOut" :icon="SwitchButton">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </el-header>
    <el-main class="layout-bottom-box">
      <div class="left">
        <el-aside width="200px" class="el-aside">
          <el-menu class="el-menu" router :default-active="$router.path">
            <el-menu-item index="/admin/system/user">
              <el-icon>
                <User/>
              </el-icon>
              <span>⽤⼾管理</span>
            </el-menu-item>
            <el-menu-item index="/admin/system/question">
              <el-icon>
                <Histogram/>
              </el-icon>
              <span>题库管理</span>
            </el-menu-item>
            <el-menu-item index="/admin/system/race">
              <el-icon>
                <Edit/>
              </el-icon>
              <span>竞赛管理</span>
            </el-menu-item>
            <!--            <el-menu-item index="/admin/system/brush">-->
            <!--              <el-icon>-->
            <!--                <Menu/>-->
            <!--              </el-icon>-->
            <!--              <span>刷题管理</span>-->
            <!--            </el-menu-item>-->
            <!--            <el-menu-item index="/admin/system/practice">-->
            <!--              <el-icon>-->
            <!--                <PieChart/>-->
            <!--              </el-icon>-->
            <!--              <span>练题管理</span>-->
            <!--            </el-menu-item>-->
            <!--            <el-menu-item index="/admin/system/discuss">-->
            <!--              <el-icon>-->
            <!--                <Operation/>-->
            <!--              </el-icon>-->
            <!--              <span>讨论管理</span>-->
            <!--            </el-menu-item>-->
            <!--            <el-menu-item index="/admin/system/interview">-->
            <!--              <el-icon>-->
            <!--                <HelpFilled/>-->
            <!--              </el-icon>-->
            <!--              <span>面试管理</span>-->
            <!--            </el-menu-item>-->
          </el-menu>
        </el-aside>
      </div>
      <div class="right">
        <RouterView/>
      </div>
    </el-main>
  </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
  height: 100vh;
  background: #f7f7f7;

  .class-header {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;

    h1 {
      font-size: 24px;
      font-weight: bold;
      color: #50c9ed;
      margin: 0;
    }
  }

  .layout-bottom-box {
    display: flex;
    justify-content: space-between;
    height: calc(100vh - 100px);
    overflow: hidden;

    .left {
      margin-right: 20px;
      background: #fff;
      display: flex;

      .el-aside {
        background: #f5f7fa; /* 确保灰色背景 */
        height: 100%; /* 填充整个父容器 */
        display: flex;
        flex-direction: column; /* 让子元素垂直排列 */
      }

      :deep(.el-menu) {
        flex: 1;

        /* 修改激活项的颜色 */
        .el-menu-item.is-active {
          color: #32c5ff;
        }

        /* 鼠标悬浮时的样式 */
        .el-menu-item:hover {
          background: #fff;
          color: #32c5ff;
        }

        /* 设置每个菜单项的间距 */
        .el-menu-item {
          margin-bottom: 10px; /* 调整菜单项的下间距 */
        }

        /* 去除最后一项的额外间距 */
        .el-menu-item:last-child {
          margin-bottom: 0;
        }
      }
    }

    .right {
      flex: 1;
      overflow-y: auto;
      background: #fff;
      padding: 20px;
    }
  }

  .el-aside {
    background-color: #fff;

    &__logo {
      height: 120px;
      // background: url('@/assets/logo.png') no-repeat center / 120px auto;
    }

    .el-menu {
      border-right: none;
    }
  }

  .el-header {
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    height: 40px;

    .el-dropdown__box {
      display: flex;
      align-items: center;

      .el-icon {
        color: #4c4141;
        margin-left: 20px;
      }

      &:active,
      &:focus {
        outline: none;
      }
    }
  }

  .el-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    color: #666;
  }
}

:deep(.el-menu) {
  flex: 1;
  background-color: #f0f4f7;
  border-radius: 8px;
  padding: 10px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);

  .el-menu-item {
    font-size: 16px;
    color: #333;
    padding: 10px 20px;
    border-radius: 6px;
    transition: all 0.3s ease;

    &:hover {
      background-color: #eaf6ff;
      color: #007bff;
    }

    &.is-active {
      background-color: #32C5FF !important;
      color: #fff !important;
      font-weight: bold;

      .el-icon {
        color: #fff !important;
      }
    }

    .el-icon {
      margin-right: 10px;
      color: #007bff;
      font-size: 20px;
      transition: color 0.3s ease;
    }
  }
}
</style>
